<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
//站点列表。需要展现的数据：站点名称，所属用户，站点logo，用户有效期（如果有）
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();

if($_RQ['op']=='list'){
	$params=array('type'=>3,'one_uniacid'=>0);
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	if(!empty($_RQ['nickname'])){
		$params['nickname like']='%'.trim($_RQ['nickname']).'%';
		$return['params']['nickname']=$_RQ['nickname'];
	}
	if(!empty($_RQ['remark'])){
		$params['remark like']='%'.trim($_RQ['remark']).'%';
		$return['params']['remark']=$_RQ['remark'];
	}
	if($_RQ['status']){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['uniacid']){
		$params['id']=intval($_RQ['uniacid']);
		$return['params']['uniacid']=$_RQ['uniacid'];
	}
	if($_RQ['endtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['endtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params2['endtime between']=array($time_range['tstart'],$time_range['tend']);
		$uniacids=pdo_getall('core_accounts',$params2,array('uniacid'),'uniacid');
		if($uniacids){
			$uniacids=array_keys($uniacids);
			$params['id']=$uniacids;
		}
	}
	$orderby='star desc,total_size desc,id desc';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 30;
	$fields=array(
		'uniacid',
		'name',
		'status',
		'nickname',
		'createtime',
		'lastvisit',
		'avatar',
		'star',
		'total_size',
		array(
			'table'=>'core_accounts',
			'join'=>array('uniacid','uniacid'),
			'fields'=>array('groupid','title','logo','endtime','stop','credit1','credit2','agent_id')
		)
	);
	$total='';
	$list=pdo_getslice('core_users',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	if($pindex==1){
		$admin=pdo_get('core_users',array('id'=>1),$fields);
		array_unshift($list,$admin);
	}
	foreach($list as &$item){
		$item['status2']=$item['status']?0:1;
		$item['nickname']=$item['nickname']?:'无';
		$item['endtime_show']=$item['endtime']?date('Y/m/d',$item['endtime']):'长期有效';
		$item['agent_name']=$item['agent_id']?pdo_getcolumn('core_users',array('id'=>$item['agent_id']),'name'):'无';
		if(!$item['total_size']){
			$total_size=pdo_fetch('select sum(`size`) from `'.TABLE_PRE.'core_attachment` where `uniacid`='.$item['uniacid']);
			$total_size=intval($total_size['sum(`size`)']/(1024*1024));
			pdo_update('core_users',array('total_size'=>$total_size),array('uniacid'=>$item['uniacid']));
		}
		$item['createtime']=date('Y/m/d',$item['createtime']);
		$item['lastvisit']=$item['lastvisit']?:$item['createtime'];
		$item['lastvisit']=date('Y/m/d',$item['lastvisit']);
		if($_SESSION['user']['isonesys']){
			$item['isonesys']=1;
		}else{
			$item['isonesys']=0;
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['statuss']=array(
		'0'=>'正常',
		'1'=>'待审核',
		'2'=>'已禁止',
	);
	$return['stops']=array('正常','禁用后台','禁用前端','整站禁用');
	$return['groups']=pdo_getall('core_account_group',array(),array('id','title'),'id');
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','psize'=>$psize,'total'=>$total,'key'=>'uniacid','edit'=>false,'del_one'=>false,
		'tip'=>'每个用户拥有一个主账号，在该帐号下，系统可以添加多个公众号和小程序，公众号和小程序的运营数据和会员数据统一在该主账号下，方便用户管理...
		</br>1、星级用户不能被清空删除！
		','list'=>array(
			//array('name'=>'uniacid','title'=>'编号','type'=>'string'),
			//array('name'=>'logo','title'=>'站点logo','type'=>'image'),
			array('name'=>'star','title'=>'星数','type'=>'star','set'=>'star'),
			array('title'=>'账号_username','type'=>'img_string','img'=>'avatar',
			'data'=>array(
				array('title'=>'','name'=>'name'),
				array('title'=>'','name'=>'nickname')
				)
			),
			array('name'=>'status2','title'=>'用户状态','type'=>'switch','set'=>'set'),
			//array('name'=>'agent_name','title'=>'推荐人','type'=>'string'),
			array('title'=>'注册/登录时间','type'=>'img_string',
			'data'=>array(
				array('title'=>'注册：','name'=>'createtime'),
				array('title'=>'登录：','name'=>'lastvisit')
				)
			),
			array('name'=>'endtime_show','title'=>'有效期/到期时间','type'=>'string'),
			array('name'=>'groupid','title'=>'账户套餐','type'=>'select','options'=>'groups'),
			//array('name'=>'credit1','title'=>'账户积分','type'=>'string'),
                array('title'=>'账户余额/容量(M)','type'=>'img_string',
                    'data'=>array(
                        array('title'=>'账户余额：','name'=>'credit2'),
                        array('title'=>'已使用容量：','name'=>'total_size')
                    )
                ),
                array('name'=>'stop','title'=>'站点状态','type'=>'radio','options'=>'stops'),
		),'action'=>array(
			array('op'=>'guanli','title'=>'管理','type'=>'href','show'=>array('isonesys'=>0)),
			array('op'=>'remark','title'=>'备注','type'=>'modal'),
			array('op'=>'perm','title'=>'套餐设置','type'=>'href','show'=>array('uniacid >'=>1)),
			array('op'=>'credit','title'=>'充值','type'=>'modal','show'=>array('uniacid >'=>1)),
			array('op'=>'password','title'=>'重置密码','type'=>'modal','show'=>array('uniacid >'=>1)),
			array('op'=>'setstop','show'=>array('uniacid >'=>1),'title'=>'设置状态','type'=>'modal'),
			array('op'=>'del','title'=>'删除','type'=>'post','show'=>array('uniacid >'=>1,'endtime <'=>TIMESTAMP,'endtime >'=>0),'message'=>'删除后该用户及所属站点将全部不可用！确定删除吗？'),
		),
		'batch'=>array(
			array('type'=>'post','msg'=>'确定删除1年前过期的站点吗？删除后附件不可找回，其他设置数据可以找回！','url'=>'core/account.list/delmore/','title'=>'清空1年前过期'),
		)
		),
		'search'=>array(
			array('type'=>'string','width'=>'10','name'=>'uniacid','title'=>'用户ID'),
			array('type'=>'string','width'=>'10','name'=>'name','title'=>'用户名'),
			array('type'=>'string','width'=>'10','name'=>'nickname','title'=>'昵称','help'=>''),
			array('type'=>'string','width'=>'10','name'=>'remark','title'=>'备注','help'=>''),
			array('type'=>'radio','width'=>'10','name'=>'status','title'=>'状态','options'=>'statuss'),
			array('type'=>'time_range','width'=>'15','name'=>'endtime','title'=>'到期时间'),
			//array('type'=>'select','width'=>'15','name'=>'groupid','title'=>'套餐','options'=>'groups'),
		),
	);
	$return['htmls']=$htmls;
	$tabs=array(
		array('op'=>'list','title'=>'用户列表'),
		array('op'=>'add','title'=>'添加用户'),
	);
	set_html('tabs',$tabs);
	exi($return);
}
if($_RQ['op']=='guanli'){
	$uniacid=intval($_RQ['uniacid']);
	if($uniacid){
		$_SESSION['uniacid']=$uniacid;
		$account=pdo_get('core_accounts',array('uniacid'=>$_SESSION['uniacid']),array('groupid','title','logo'));
		$_SESSION['account']=$account;
		$_SESSION['account']['noshow_app']='';
		$_SESSION['account']['noshow_mainmenus']='';
		set_html('account',$account);
		$url='/account.siteinfo/';
		if($account['groupid']){			
			$group=pdo_get('core_account_group',array('id'=>$account['groupid']),array('plugin_name','index_url','noshow_app','noshow_mainmenus'));
			if($group['index_url']){
				$url=$group['index_url'];
			}
			if($group['plugin_name']){
				$_SESSION['current_plugin']=$group['plugin_name'];
			}
			$_SESSION['account']['noshow_app']=$group['noshow_app']?:'';
			$_SESSION['account']['noshow_mainmenus']=$group['noshow_mainmenus']?:'';
		}
		exi('管理成功！','',manage_url($url,true));
	}
	exi('管理站点失败，请联系站点管理员！');
}
if($_RQ['op']=='edit'){
	$uniacid=intval($_RQ['uniacid']);
	if($uniacid){
		$_SESSION['uniacid']=$uniacid;
		$account=pdo_get('core_accounts',array('uniacid'=>$_SESSION['uniacid']),array('title','logo'));
		set_html('account',$account);
		exi('','','/setting.site/');
	}
	exi('管理站点失败，请联系站点管理员！');
}
if($_RQ['op']=='setstop'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$update=array(
			'stop'=>intval($post['stop']),
		);
		$uniacid=intval($post['uniacid']);
		pdo_update('core_accounts',$update,array('uniacid'=>$uniacid));
		exi('设置成功！','','list');
	}else{
		$uniacid=intval($_RQ['uniacid']);
		if(!$uniacid){
			exi('请先选择要管理的站点！','error');
		}
		$item=pdo_get('core_accounts',array('uniacid'=>$uniacid),array('uniacid','title','stop'));
		$return['item']=$item;
		$return['htmls']=array(
			'type'=>'edit',
			'desc'=>'站点状态',
			'edit'=>array(
				array('type'=>'copy','name'=>'title','title'=>'站点名称'),
				array('type'=>'radio','name'=>'stop','title'=>'站点状态','options'=>'stops','help'=>'【<red>禁用后台</red>】：该站点下的所有账号均无法登陆后台！但是站点页面依然可以访问！'),
			),
		);
		$return['stops']=array('正常','禁用后台','禁用前端','整站禁用');
		exi($return);
	}	
}
if($_RQ['op']=='perm'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$update=array(
			'groupid'=>intval($post['groupid']),
			'attachment'=>intval($post['attachment']),
			'endtime'=>strtotime($post['endtime']),
		);
		$uniacid=intval($post['uniacid']);
		pdo_update('core_accounts',$update,array('uniacid'=>$uniacid));
		exi('设置成功！','','list');
	}else{
		$uniacid=intval($_RQ['uniacid']);
		if(!$uniacid){
			exi('请先选择要管理的站点！','error');
		}
		$item=pdo_get('core_accounts',array('uniacid'=>$uniacid),array('uniacid','title','groupid','endtime','credit1','credit2','attachment'));
		$group=pdo_get('core_account_group',array('id'=>$item['groupid']));
		$item['endtime']=date('20y-m-d H:i',$item['endtime']);
		$item['resource']=$group['resource'];
		$return['item']=$item;
		$return['htmls']=array(
			'type'=>'edit',
			'desc'=>'管理站点信息',
			'edit'=>array(
				array('type'=>'tip','title'=>'[温馨提示]：<red>给客户变更套餐后，需要客户退出重新登录，才能生效</red>！'),
				array('type'=>'string','name'=>'title','title'=>'站点名称','read'=>true),
				array('type'=>'string','name'=>'credit1','title'=>'账户积分','read'=>true),
				array('type'=>'string','name'=>'credit2','title'=>'账户余额','read'=>true),
				array('type'=>'select','name'=>'groupid','title'=>'站点套餐','options'=>'groups'),
				array('type'=>'unitstring','unit'=>'M','name'=>'resource','title'=>'套餐容量','read'=>true,'help'=>'如果修改套餐，保存后这里才会刷新！'),
				array('type'=>'unitstring','unit'=>'M','name'=>'attachment','title'=>'扩容容量','help'=>'站点容量=套餐容量+扩容容量'),
				array('type'=>'time','name'=>'endtime','title'=>'有效期'),
				
			),
		);
		$return['groups']=pdo_getall('core_account_group',array(),array('id','title'));
		exi($return);
	}	
}
if($_RQ['op']=='add'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$user=array(
			'name'=>trim($post['name']),
			'password'=>trim($post['password']),
			'groupid'=>intval($post['groupid']),
			'endtime'=>strtotime($post['endtime']),
			'credit1'=>intval($post['credit1']),
			'credit2'=>intval($post['credit2']),
		);
		$check=pdo_getcolumn('core_users',array('name'=>$user['name']),'id');
		if($check){
			exi('用户名已存在，请更换！','error');
		}
		cfc('user')->create_user($user);
		exi('创建成功！','','list');
	}else{
		
		$return['item']=array();
		$return['htmls']=array(
			'type'=>'edit',
			'desc'=>'管理站点信息',
			'edit'=>array(
				array('type'=>'string','name'=>'name','title'=>'用户账号'),
				array('type'=>'string','name'=>'password','title'=>'用户密码'),
				array('type'=>'select','name'=>'groupid','title'=>'套餐','options'=>'groups'),
				array('type'=>'time','name'=>'endtime','title'=>'有效期'),
				array('type'=>'string','name'=>'credit1','title'=>'账户积分'),
				array('type'=>'string','name'=>'credit2','title'=>'账户余额'),
			),
		);
		$return['groups']=pdo_getall('core_account_group',array(),array('id','title'));
		exi($return);
	}
	
}
if($_RQ['op']=='del'){
	//删除站点，先导出所有站点数据保存，然后删除站点数据
	//检索所有的表，如果表里面有字段uniacid，则删除相关数据
	$dels=array();
	if($_RQ['del']){
		$dels=str_array($_RQ['del']);
	}
	$uniacid=intval($_RQ['uniacid']);
	if($uniacid){
		$dels[]=$uniacid;
	}
	if(!$dels){
		exi('没有选择站点','error');
	}
	foreach($dels as $uniacid){
		if($uniacid==1){
			exi('创始人账户不支持删除！','error');
		}
		$account=pdo_get('core_accounts',array('uniacid'=>$uniacid),array('title'));
		if(!$account){
			exi('要删除的站点不存在！','error');
		}
		$isstar=pdo_getcolumn('core_users',array('id'=>$uniacid),'star');
		if($isstar){
			exi('存在星级用户不能删除！','error');
		}
		$ignores=array('core_settings','core_cache','core_session','core_plugins','core_user_failed_login');
		$tables=pdo_fetchall("SHOW TABLES LIKE '".TABLE_PRE."core_%'");
		$data=array();
		$del=array();
		foreach($tables as $table){
			$tablename=reset($table);
			$name=explode('_',$tablename,2);
			$name=$name['1'];
			if(pdo_fieldexists($name,'uniacid')){
				$data[$name]=pdo_getall($name,array('uniacid'=>$uniacid));
				if($data[$name]){
					$del[]=$name;
				}
			}
		}
		$data=serialize($data);
		$filename=cfc('files')->source_save($data,'account_bak','bak','删除站点“'.$account['title'].'”后的备份数据');
		$bak=array('title'=>$account['title'],'uniacid'=>$uniacid,'reason'=>'管理员'.$_SESSION['user']['name'].'手动删除','file'=>$filename,'createtime'=>TIMESTAMP);
		pdo_insert('core_account_recycle',$bak);
		foreach($del as $name){
			pdo_delete($name,array('uniacid'=>$uniacid));
		}
		//删除站点附件
		$path=ROOT_AT .'oss/' . $uniacid;
		cfc('files')->rmdirs($path);
		$attachment=pdo_getall('core_attachment',array('uniacid'=>$uniacid),array('id','attachment'));
		foreach($attachment as $item){
			@unlink(ROOT_AT.$item['attachment']);
		}
		pdo_delete('core_attachment',array('uniacid'=>$uniacid));
		cfc('corelog')->oper_log('删除站点',$uniacid);
	}
	exi('删除成功！');
}
if($_RQ['op']=='delmore'){
	//删除站点，先导出所有站点数据保存，然后删除站点数据
	//检索所有的表，如果表里面有字段uniacid，则删除相关数据
	$params=array('uniacid >'=>1);
	$tstart=strtotime('2016-01-01');
	$tend=strtotime("-12 month");
	$check=strtotime('2021-01-01');
	if($tend>$check){
		exi('删除异常！','error');
	}
	$params['endtime between']=array($tstart,$tend);
	$accounts=pdo_getall('core_accounts',$params,array('uniacid','title','endtime'));
	foreach($accounts as $item){
		if($item['uniacid']==1){
			exi('创始人账户不支持删除！','error');
		}
		if(!$item['uniacid']){
			exi('清空保护，异常操作！','error');
		}
		$isstar=pdo_getcolumn('core_users',array('id'=>$item['uniacid']),'star');
		if($isstar){
			exi('存在星级用户不能删除！','error');
		}
		$ignores=array('core_settings','core_cache','core_session','core_plugins','core_user_failed_login');
		$tables=pdo_fetchall("SHOW TABLES LIKE '".TABLE_PRE."core_%'");
		$data=array();
		$del=array();
		foreach($tables as $table){
			$tablename=reset($table);
			$name=explode('_',$tablename,2);
			$name=$name['1'];
			if(pdo_fieldexists($name,'uniacid')){
				$data[$name]=pdo_getall($name,array('uniacid'=>$item['uniacid']));
				if($data[$name]){
					$del[]=$name;
				}
			}
		}
		$data=serialize($data);
		$filename=cfc('files')->source_save($data,'account_bak','bak','删除站点“'.$item['title'].'”后的备份数据');
		$bak=array('title'=>$item['title'],'uniacid'=>$item['uniacid'],'reason'=>'管理员'.$_SESSION['user']['name'].'手动删除','file'=>$filename,'createtime'=>TIMESTAMP);
		pdo_insert('core_account_recycle',$bak);
		foreach($del as $name){
			pdo_delete($name,array('uniacid'=>$item['uniacid']));
		}
		cfc('corelog')->oper_log('删除站点',$item['uniacid']);
	}
	exi('删除成功！');
}
if($_RQ['op']=='credit'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$credit=array(
			'type'=>intval($post['type']),
			'is_add'=>intval($post['is_add']),
			'fee'=>intval($post['fee']),
			'remark'=>"管理员{$_SESSION['user']['name']}【{$_SESSION['uid']}】手动操作：{$post['remark']}",
		);
		$uniacid=intval($post['uniacid']);
		model('core/web',$uniacid)->account_credit($credit);
		exi('操作成功！','','list');
	}else{
		$uniacid=intval($_RQ['uniacid']);
		if(!$uniacid){
			exi('请先选择要充值的用户！','error');
		}
		$item=pdo_get('core_accounts',array('uniacid'=>$uniacid),array('uniacid','title','credit1','credit2'));
		$item['name']=pdo_getcolumn('core_users',array('id'=>$uniacid),'name');
		$return['item']=$item;
		$return['htmls']=array(
			'type'=>'edit',
			'edits'=>array(
				array('title'=>'用户信息','list'=>array(
					array('type'=>'string','name'=>'title','title'=>'站点名称','read'=>true),
					array('type'=>'string','name'=>'name','title'=>'主账户','read'=>true),
					array('type'=>'string','name'=>'credit1','title'=>'账户当前积分','read'=>true),
					array('type'=>'string','name'=>'credit2','title'=>'账户当前余额','read'=>true),
				)),
				array('title'=>'充值设置','list'=>array(
					array('type'=>'radio','name'=>'type','title'=>'积分类型','options'=>'types'),
					array('type'=>'radio','name'=>'is_add','title'=>'修改方式','options'=>'is_adds'),
					array('type'=>'string','name'=>'fee','title'=>'修改金额'),
					array('type'=>'text','name'=>'remark','title'=>'备注'),
				)),
			),
		);
		$return['types']=array('1'=>'积分','2'=>'余额');
		$return['is_adds']=array('0'=>'减少','1'=>'增加');
		exi($return);
	}
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['uniacid']);
	if(!$id){
		exi('参数错误！','error');
	}
	if($id==1){
		exi('创始人账户不支持设置！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['status2'])){
		$update['status']=$post['status2']?0:1;
	}
	if($update){
		pdo_update('core_users',$update,array('id'=>$id));
	}
	exi('操作成功！');
}
if($_RQ['op']=='star'){
	$id=intval($_RQ['uniacid']);
	if(!$id){
		exi('参数错误！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['star'])){
		$update['star']=$post['star'];
	}
	if($update){
		pdo_update('core_users',$update,array('id'=>$id));
	}
	exi('操作成功！');
}
if($_RQ['op']=='password'){
	//重置密码
	if($_RQ['postdata']){
		$post=get_postdata();
		if($post['passwd1']!=$post['passwd2']){
			exi('两次输入密码不一致！','error');
		}
		$update=array('salt'=>random(8));
		$update['password']=get_password($post['passwd1'],$update['salt']);
		pdo_update('core_users',$update,array('id'=>intval($post['id'])));
		
		exi('操作成功！');
	}else{
		$uniacid=intval($_RQ['uniacid']);
		if(!$uniacid){
			exi('请先选择用户！','error');
		}
		$item=pdo_get('core_users',array('id'=>$uniacid),array('id','name'));
		$return['item']=$item;
		$return['htmls']=array(
			'type'=>'edit',
			'desc'=>'重置用户密码',
			'edit'=>array(
				array('type'=>'string','name'=>'name','title'=>'用户账号','read'=>true),
				array('type'=>'password','name'=>'passwd1','title'=>'输入密码'),
				array('type'=>'password','name'=>'passwd2','title'=>'再次输入密码'),
			),
		);
		exi($return);
	}
}
if($_RQ['op']=='remark'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$update=array(
			'remark'=>$post['remark'],
		);
		$uniacid=intval($post['uniacid']);
		pdo_update('core_users',$update,array('uniacid'=>$uniacid));
		exi('设置成功！','','list');
	}else{
		$uniacid=intval($_RQ['uniacid']);
		if(!$uniacid){
			exi('请先选择要备注的用户！','error');
		}
		$item=pdo_get('core_users',array('uniacid'=>$uniacid),array('uniacid','remark','name'));
		$return['item']=$item;
		$return['htmls']=array(
			'type'=>'edit',
			'desc'=>'用户备注',
			'edit'=>array(
				array('type'=>'string','name'=>'name','title'=>'站点名称','read'=>true),
				array('type'=>'string','name'=>'remark','title'=>'备注'),
			),
		);
		exi($return);
	}	
}
?>